package com.freesky.quartz.task;

import cn.hutool.core.date.DateUtil;
import com.freesky.biz.activity.domain.TCouponBill;
import com.freesky.biz.activity.domain.TCouponDetail;
import com.freesky.biz.activity.mapper.CouponBillMapper;
import com.freesky.biz.psbc.domain.TPsbcCoupon;
import com.freesky.biz.psbc.domain.TPsbcLotteryTicket;
import com.freesky.biz.psbc.mapper.PsbcCouponMapper;
import com.freesky.biz.psbc.mapper.PsbcLotteryTicketMapper;
import com.freesky.biz.psbc.service.PsbcCommService;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.redis.RedisCache;
import com.freesky.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 邮储支付宝券码核销同步
 */
@Component("psbcSyncCouponTask")
public class PsbcSyncCouponTask {

    private static final Logger logger = LoggerFactory.getLogger(PsbcSyncCouponTask.class);

    @Autowired
    private PsbcCouponMapper psbcCouponMapper;

    @Autowired
    private CouponBillMapper couponBillMapper;

    @Autowired
    private PsbcCommService psbcCommService;

    @Autowired
    private RedisCache redisCache;

    private String PSBC_SYNC_COUPON_KEY = "PSBC:SYNC:COUPON";

    private String PSBC_SYNC_WEIXIN_COUPON_KEY = "PSBC:SYNC:WEIXIN_COUPON";

    public void taskProcess() {
        logger.info("psbcSyncCouponTask task start");
        String consumeTime = redisCache.getCacheObject(PSBC_SYNC_COUPON_KEY);
        if (StringUtils.isEmpty(consumeTime)) {
            consumeTime = "2024-01-01";
        }

        String weixinConsumeTime = redisCache.getCacheObject(PSBC_SYNC_WEIXIN_COUPON_KEY);
        if (StringUtils.isEmpty(weixinConsumeTime)) {
            weixinConsumeTime = "2024-01-01";
        }

        try {
            List<TPsbcCoupon> psbcCouponList = psbcCouponMapper.selectUnPushList(consumeTime);
            for (TPsbcCoupon psbcCoupon : psbcCouponList) {
                if (StringUtils.isNotEmpty(psbcCoupon.getOrderNo())) {
                    AjaxResult ajaxResult = psbcCommService.commService(psbcCoupon.getOrderNo(), psbcCoupon.getThirdCpnId(), "01");
                    if (ajaxResult.isSuccess()) {
                        psbcCouponMapper.updatePushFlag(psbcCoupon.getId(), "1", "成功");
                    } else {
                        psbcCouponMapper.updatePushFlag(psbcCoupon.getId(), "2", ajaxResult.getMsg());
                    }
                }
                redisCache.setCacheObject(PSBC_SYNC_COUPON_KEY, psbcCoupon.getUpdateTime());
            }

            List<TCouponBill> couponDetailList = couponBillMapper.selectUnPushList(weixinConsumeTime);
            for (TCouponBill couponBill : couponDetailList) {
                String consumeMchId = couponBill.getConsumeMchid();
                if(StringUtils.isEmpty(consumeMchId)){
                    consumeMchId = "1558805731";
                }

                String storeCode = couponBill.getCosumeCode1();
                if(StringUtils.isEmpty(storeCode)){
                    storeCode = consumeMchId;
                }

                String consumeTime2 = DateUtil.format(DateUtil.parse(couponBill.getConsumeTime(),"yyyy-MM-dd HH:mm:ss"),"yyyyMMddHHmmss");

                AjaxResult pushResult = psbcCommService.weixinStock(couponBill.getBankNo(), couponBill.getProductInfo(),
                        couponBill.getStockId(),
                        couponBill.getCouponId(), "USED", consumeMchId, consumeTime2,storeCode);
                if (pushResult.isSuccess()) {
                    couponBillMapper.updatePushFlag(couponBill.getId(),"1",pushResult.getMsg());
                } else {
                    couponBillMapper.updatePushFlag(couponBill.getId(),"2",pushResult.getMsg());
                }
                redisCache.setCacheObject(PSBC_SYNC_WEIXIN_COUPON_KEY, couponBill.getUpdateTime());
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("psbcSyncCouponTask task error", e);
        }
    }
}
